<?php

namespace App\Http\Controllers\V1;

use App\Http\Controllers\ApiBaseController;
use App\Http\Requests\UserGroupRequest;
use App\Models\UserGroup;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;


class UserGroupController extends ApiBaseController
{
    public function index(Request $request){
        $paginator = UserGroup::filter($request->all())->paginate($this->pageSize);
        return success(page_format($paginator));
    }

    public function listAll(Request $request){
        $items = UserGroup::all();
        return success(['data' => $items]);
    }

    public function show(Request $request, UserGroup $model){
        $id = $request->get('id',0);
        $data = $model->find($id);
        return success([
            'data' => $data,
        ]);
    }

    public function create(UserGroupRequest $request){
        $params = $request->all();

        DB::transaction(function () use ($params) {
            $userGroup = UserGroup::create($params);
        });
        return success([],'更新成功');
    }

    public function update(UserGroupRequest $request, UserGroup $model)
    {
        $params = $request->all();
        $id = $request->post('id');
        $group = $model->find($id);
        if(!$group){
            return error('数据不存在');
        }
        DB::transaction(function () use ($params, $group) {
            $group->update($params);
        });
        return success([],'更新成功');
    }

    public function destroy($ids, Request $request, UserGroup $model){
        $id_arr = explode(',', $ids);
        $model->destroy($id_arr);
        return success([],'删除成功');

    }

}
